package com.github.itdachen.controller;

import com.github.itdachen.utils.SSEMsgType;
import com.github.itdachen.utils.SSEServer;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;

/**
 * SSEController
 *
 * @author 王大宸
 * @date 2025/9/15 16:38
 */
@Slf4j
@RestController
@RequestMapping("sse")
public class SSEController {


    /***
     * 连接sse服务的接口
     *
     * @author 王大宸
     * @date 2025/9/15 21:53
     * @param userId userId
     * @return org.springframework.web.servlet.mvc.method.annotation.SseEmitter
     */
    @GetMapping(path = "connect", produces = {MediaType.TEXT_EVENT_STREAM_VALUE})
    public SseEmitter connect(@RequestParam String userId) {
        return SSEServer.connect(userId);
    }

    /***
     * 发送单条消息给 SSE 的客户端
     * http://127.0.0.1:8080/sse/sendMessage?userId=1001&message=这是一条消息
     * @author 王大宸
     * @date 2025/9/15 21:53
     * @param userId userId
     * @param message message
     * @return java.lang.Object
     */
    @GetMapping("sendMessage")
    public Object sendMessage(@RequestParam String userId, @RequestParam String message) {
        SSEServer.sendMessage(userId, message, SSEMsgType.MESSAGE);
        return "OK";
    }

    /***
     * 发送消息给所有客户端用户
     * http://127.0.0.1:8080/sse/sendMessageAll?message=发送给所有人~10086
     * @author 王大宸
     * @date 2025/9/15 21:53
     * @param message message
     * @return java.lang.Object
     */
    @GetMapping("sendMessageAll")
    public Object sendMessageAll(@RequestParam String message) {
        SSEServer.sendMessageToAllUsers(message);
        return "OK";
    }

    /***
     * add事件流式输出
     * http://127.0.0.1:8080/sse/sendMessageAdd?userId=xeah0ozu3&message=ADD事件流式输出
     * @author 王大宸
     * @date 2025/9/15 21:54
     * @param userId userId
     * @param message message
     * @return java.lang.Object
     */
    @GetMapping("sendMessageAdd")
    public Object sendMessageAdd(@RequestParam String userId,
                                 @RequestParam String message) throws Exception {
        for (int i = 0; i < 10; i++) {
            Thread.sleep(200);
            SSEServer.sendMessage(userId, message + "-" + i, SSEMsgType.ADD);
        }
        return "OK";
    }

    /***
     * 停止 sse
     * http://127.0.0.1:8080/sse/stop?userId=xeah0ozu3
     * @author 王大宸
     * @date 2025/9/15 21:54
     * @param userId userId
     * @return java.lang.Object
     */
    @GetMapping("stop")
    public Object stopServer(@RequestParam String userId) {
        SSEServer.stopServer(userId);
        return "OK";
    }

    /***
     * 获得当前所有的会话总连接数（在线人数）
     * http://127.0.0.1:8080/sse/getOnlineCounts
     *
     * @author 王大宸
     * @date 2025/9/15 21:54
     * @return java.lang.Object
     */
    @GetMapping("getOnlineCounts")
    public Object getOnlineCounts() {
        return SSEServer.getOnlineCounts();
    }


}
